/**
 * 
 */
package com.swim.settlementapply.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.swim.settlementapply.entity.SettlementApply;
import com.swim.settlementapply.entity.SettlementApplyCount;
import com.swim.settlementapply.entity.SettlementApplyItem;
import com.swim.natatoriuminfo.entity.NatatoriumInfo;
import com.swim.natatoriuminfo.service.NatatoriumInfoService;
import com.swim.natatoriuminfotouser.entity.NatatoriumInfoToUser;
import com.swim.natatoriuminfotouser.service.NatatoriumInfoToUserService;
import com.swim.natatoriumorder.entity.NatatoriumOrder;
import com.swim.natatoriumorder.service.NatatoriumOrderService;
import com.swim.settlementapply.dao.SettlementApplyDao;
import com.swim.settlementapply.dao.SettlementApplyItemDao;

/**
 * 结算申请Service
 * @author 刘军武
 * @version 2021-03-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SettlementApplyService extends CrudService<SettlementApplyDao, SettlementApply> {
	
	@Autowired
	private NatatoriumInfoToUserService natatoriumInfoToUserService;
	
	@Autowired
	private NatatoriumOrderService natatoriumOrderService;
	
	@Autowired
	private NatatoriumInfoService natatoriumInfoService;
	
	@Autowired
	private SettlementApplyItemDao settlementApplyItemDao;
	
	/**
	 * 获取单条数据
	 * @param settlementApply
	 * @return
	 */
	@Override
	public SettlementApply get(SettlementApply settlementApply) {
		return super.get(settlementApply);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param settlementApply
	 * @return
	 */
	@Override
	public Page<SettlementApply> findPage(Page<SettlementApply> page, SettlementApply settlementApply) {
		return super.findPage(page, settlementApply);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param settlementApply
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(SettlementApply settlementApply) {
		super.save(settlementApply);
	}
	
	/**
	 * 更新状态
	 * @param settlementApply
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(SettlementApply settlementApply) {
		super.updateStatus(settlementApply);
	}
	
	/**
	 * 删除数据
	 * @param settlementApply
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(SettlementApply settlementApply) {
		super.delete(settlementApply);
	}
	
	/**
	 * 获取当前申请结算统计数据
	 * @param settlementApply
	 */
	public SettlementApplyCount getSettlementApplyCount() {
		SettlementApplyCount settlementApplyCount = new SettlementApplyCount();
		//查询所属游泳馆
		NatatoriumInfoToUser natatoriumInfoToUser = new NatatoriumInfoToUser();
		natatoriumInfoToUser.setUserCode(UserUtils.getUser().getUserCode());
		List<NatatoriumInfoToUser> natatoriumInfoToUserList = natatoriumInfoToUserService.findList(natatoriumInfoToUser);
		NatatoriumInfo natatoriumInfo = natatoriumInfoService.get(natatoriumInfoToUserList.get(0).getNatatoriumInfoId());
		//查询可结算首款的订单
		NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
		natatoriumOrder.setNatatoriumInfoId(natatoriumInfo.getId());
		natatoriumOrder.setSettlementStatus(3L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
		List<NatatoriumOrder> natatoriumOrderFirstList = natatoriumOrderService.findList(natatoriumOrder);
		settlementApplyCount.setFirstOrderNum(CollectionUtils.isEmpty(natatoriumOrderFirstList) ? 0 : natatoriumOrderFirstList.size());//首次结算订单数
		Double firstMoney = 0.0;		//首次结算金额
		if (!CollectionUtils.isEmpty(natatoriumOrderFirstList)) {
			for (NatatoriumOrder natatoriumOrderOne : natatoriumOrderFirstList) {
				//应付金额 * 首次结算比例
				firstMoney = firstMoney + natatoriumOrderOne.getShouldPay() * (natatoriumInfo.getSettlementFirst()/100);
			}
		}
		settlementApplyCount.setFirstMoney(firstMoney);
		
		//查询可结算尾款的订单
		natatoriumOrder.setSettlementStatus(4L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
		List<NatatoriumOrder> natatoriumOrderLastList = natatoriumOrderService.findList(natatoriumOrder);
		settlementApplyCount.setLastOrderNum(CollectionUtils.isEmpty(natatoriumOrderLastList) ? 0 : natatoriumOrderLastList.size());//最后结算订单数  
		Double lastMoney = 0.0;    //最后结算金额 
		if (!CollectionUtils.isEmpty(natatoriumOrderLastList)) {
			for (NatatoriumOrder natatoriumOrderOne : natatoriumOrderLastList) {
				//应付金额 * 最后结算比例
				lastMoney = lastMoney + natatoriumOrderOne.getShouldPay() * (natatoriumInfo.getSettlementLast()/100);
			}
		}
		settlementApplyCount.setLastMoney(lastMoney);
		
		//查询可结算状态的游泳票订单
		natatoriumOrder.setSettlementStatus(1L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
		List<NatatoriumOrder> natatoriumOrderSwimmingTicketList = natatoriumOrderService.findList(natatoriumOrder);
		settlementApplyCount.setSwimmingTicketOrderNum(CollectionUtils.isEmpty(natatoriumOrderSwimmingTicketList) ? 0 : natatoriumOrderSwimmingTicketList.size());//游泳票订单数    
		Double swimmingTicketMoney = 0.0;    //游泳票金额 
		if (!CollectionUtils.isEmpty(natatoriumOrderSwimmingTicketList)) {
			for (NatatoriumOrder natatoriumOrderOne : natatoriumOrderSwimmingTicketList) {
				//应付金额叠加
				swimmingTicketMoney = swimmingTicketMoney + natatoriumOrderOne.getShouldPay();
			}
		}
		settlementApplyCount.setSwimmingTicketMoney(swimmingTicketMoney);
		
		Double allMoney = firstMoney + lastMoney + swimmingTicketMoney;//总金额
		settlementApplyCount.setAllMoney(allMoney);
		
		settlementApplyCount.setSettlementFirst(natatoriumInfo.getSettlementFirst());		// 首次结算比列
		settlementApplyCount.setSettlementLast(natatoriumInfo.getSettlementLast());		// 最后结算比列
		return settlementApplyCount;
	}
	
	/**
	 * 申请结算
	 */
	public void settlementApply() {
		SettlementApplyCount settlementApplyCount = new SettlementApplyCount();
		List<SettlementApplyItem> settlementApplyItemList = new ArrayList<SettlementApplyItem>();
		//查询所属游泳馆
		NatatoriumInfoToUser natatoriumInfoToUser = new NatatoriumInfoToUser();
		natatoriumInfoToUser.setUserCode(UserUtils.getUser().getUserCode());
		List<NatatoriumInfoToUser> natatoriumInfoToUserList = natatoriumInfoToUserService.findList(natatoriumInfoToUser);
		NatatoriumInfo natatoriumInfo = natatoriumInfoService.get(natatoriumInfoToUserList.get(0).getNatatoriumInfoId());
		
		//查询可结算首款的订单
		NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
		natatoriumOrder.setNatatoriumInfoId(natatoriumInfo.getId());
		natatoriumOrder.setSettlementStatus(3L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
		List<NatatoriumOrder> natatoriumOrderFirstList = natatoriumOrderService.findList(natatoriumOrder);
		Double firstMoney = 0.0;		//首次结算金额
		int natatoriumOrderFirstNum = 0; //可结算首款的订单数
		if (!CollectionUtils.isEmpty(natatoriumOrderFirstList)) {
			natatoriumOrderFirstNum = natatoriumOrderFirstList.size();
			for (NatatoriumOrder natatoriumOrderOne : natatoriumOrderFirstList) {
				//叠加首次结算金额
				firstMoney = firstMoney + natatoriumOrderOne.getShouldPay() * (natatoriumInfo.getSettlementFirst()/100);
				//结算金额
				Double amount = natatoriumOrderOne.getShouldPay() * (natatoriumInfo.getSettlementFirst()/100);
				natatoriumOrderOne.setActualSettlement(amount);		// 已结算金额
				natatoriumOrderOne.setSettlementStatus(5L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
				natatoriumOrderService.update(natatoriumOrderOne);
				//记录结算申请明细
				SettlementApplyItem settlementApplyItem = new SettlementApplyItem();
				settlementApplyItem.setOrderNum(natatoriumOrderOne.getOrderNum());		// 订单号  
				settlementApplyItem.setOrderMoney(natatoriumOrderOne.getShouldPay());		// 订单金额            
				settlementApplyItem.setSettlementType("首款" + natatoriumInfo.getSettlementFirst() + "%");		// 结算类型        
				settlementApplyItem.setSettlementMoney(amount);		// 结算金额
				settlementApplyItemList.add(settlementApplyItem);
			}
		}
		settlementApplyCount.setFirstMoney(firstMoney);
		
		//查询可结算尾款的订单
		natatoriumOrder.setSettlementStatus(4L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
		List<NatatoriumOrder> natatoriumOrderLastList = natatoriumOrderService.findList(natatoriumOrder);
		Double lastMoney = 0.0;    //最后结算金额 
		int natatoriumOrderLastNum = 0; //可结算尾款的订单数
		if (!CollectionUtils.isEmpty(natatoriumOrderLastList)) {
			natatoriumOrderLastNum = natatoriumOrderLastList.size();
			for (NatatoriumOrder natatoriumOrderOne : natatoriumOrderLastList) {
				//叠加最后结算金额
				lastMoney = lastMoney + natatoriumOrderOne.getShouldPay() * (natatoriumInfo.getSettlementLast()/100);
				//结算金额
				Double amount = natatoriumOrderOne.getShouldPay() * (natatoriumInfo.getSettlementLast()/100);
				natatoriumOrderOne.setActualSettlement(amount + natatoriumOrderOne.getActualSettlement());		// 已结算金额
				natatoriumOrderOne.setSettlementStatus(6L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
				natatoriumOrderService.update(natatoriumOrderOne);
				//记录结算申请明细
				SettlementApplyItem settlementApplyItem = new SettlementApplyItem();
				settlementApplyItem.setOrderNum(natatoriumOrderOne.getOrderNum());		// 订单号             
				settlementApplyItem.setOrderMoney(natatoriumOrderOne.getShouldPay());		// 订单金额            
				settlementApplyItem.setSettlementType("尾款" + natatoriumInfo.getSettlementLast() + "%");		// 结算类型        
				settlementApplyItem.setSettlementMoney(amount);		// 结算金额  
				settlementApplyItemList.add(settlementApplyItem);				
			}
		}
		settlementApplyCount.setLastMoney(lastMoney);
		
		//查询可结算状态的游泳票订单
		natatoriumOrder.setSettlementStatus(1L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
		List<NatatoriumOrder> natatoriumOrderSwimmingTicketList = natatoriumOrderService.findList(natatoriumOrder);
		Double swimmingTicketMoney = 0.0;    //游泳票金额 
		int natatoriumOrderSwimmingTicketNum = 0; //可结算状态的游泳票订单数		
		if (!CollectionUtils.isEmpty(natatoriumOrderSwimmingTicketList)) {
			natatoriumOrderSwimmingTicketNum = natatoriumOrderSwimmingTicketList.size();
			for (NatatoriumOrder natatoriumOrderOne : natatoriumOrderSwimmingTicketList) {
				//应付金额叠加
				swimmingTicketMoney = swimmingTicketMoney + natatoriumOrderOne.getShouldPay();
				natatoriumOrderOne.setSettlementStatus(6L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
				natatoriumOrderService.update(natatoriumOrderOne);
				//记录结算申请明细
				SettlementApplyItem settlementApplyItem = new SettlementApplyItem();
				settlementApplyItem.setOrderNum(natatoriumOrderOne.getOrderNum());		// 订单号             
				settlementApplyItem.setOrderMoney(natatoriumOrderOne.getShouldPay());		// 订单金额            
				settlementApplyItem.setSettlementType("游泳票");		// 结算类型        
				settlementApplyItem.setSettlementMoney(natatoriumOrderOne.getShouldPay());		// 结算金额   
				settlementApplyItemList.add(settlementApplyItem);				
			}
		}
		
		//新增结算申请
		SettlementApply settlementApply = new SettlementApply();
		settlementApply.setNatatoriumInfoId(natatoriumInfo.getId());		// 游泳馆信息id
		settlementApply.setChannel("游泳馆");		// 渠道                            
		settlementApply.setWhatsoever(natatoriumInfo.getName());		// 申请方                       
		settlementApply.setName(UserUtils.getUser().getUserName());		// 姓名                            
		settlementApply.setContact(UserUtils.getUser().getUserCode());		// 联系方式               
		String settlementOverview = "培训课程" + natatoriumInfo.getSettlementFirst() + "%:" + natatoriumOrderFirstNum + "订单，培训课程" +
				natatoriumInfo.getSettlementLast() + "%:" + natatoriumOrderLastNum + "订单，游泳票:" + natatoriumOrderSwimmingTicketNum + "订单";
		settlementApply.setSettlementOverview(settlementOverview);		// 结算概览              
		settlementApply.setSettlementAmount(firstMoney + lastMoney + swimmingTicketMoney);		// 结算金额              
		settlementApply.setAddress(natatoriumInfo.getAddress());		// 场馆地址                          
		settlementApply.setSettlementApplyState(1L);		// 申请状态 1表示结算中 2表示已结算
		super.save(settlementApply);
		
		//批量新增 记录结算申请明细数据
		if (!CollectionUtils.isEmpty(settlementApplyItemList)) {
			for (SettlementApplyItem settlementApplyItem : settlementApplyItemList) {
				settlementApplyItem.setSettlementApplyId(settlementApply.getId());		// 结算申请表id 
			}
			settlementApplyItemDao.insertBatch(settlementApplyItemList);
		}
	}
}